<?php

namespace App\Formats;

use App\Common\Libs\ArrayHelper;
use App\Common\Libs\IdBuilder;
use App\Consts\GlobalConst;
use App\Models\Field\UserField;
use App\Models\SysDynamic;

class DynamicFormat extends BaseFormat
{
    public const SQUARE_ATTENTION_LIST = 'squareAttentionList';

    public const USER_DYNAMIC_LIST = 'userDynamicList';

    public function formatRecommendSystemList($dynamics, $currentUserId, $formatScene)
    {
        switch ($formatScene) {
            case self::SQUARE_ATTENTION_LIST:
                return self::formatBySquareAttentionList($dynamics, $currentUserId);
            case self::USER_DYNAMIC_LIST:
                return self::formatByUserDynamicList($dynamics, $currentUserId);
        }

        return [];
    }

    public static function formatBySquareAttentionList($attentions, $currentUserId)
    {
        $dynamics = self::getDynamicArr(ArrayHelper::getColumn($attentions, 'target_id'), ArrayHelper::getColumn($attentions, 'user_id'));

        $articleList = formats()->ArticleFormat->formatArticleList(ArrayHelper::getColumn($dynamics, 'target_id'), $currentUserId, ArticleFormat::SQUARE_ATTENTION_LIST);
        $articleList = ArrayHelper::index($articleList, 'id');

        $noteList = formats()->NoteFormat->formatNoteList(ArrayHelper::getColumn($dynamics, 'target_id'), $currentUserId, NoteFormat::SQUARE_RECOMMEND_LIST);
        $noteList = ArrayHelper::index($noteList, 'id');

        $userIds = ArrayHelper::getColumn($attentions, 'user_id');
        $userArr = UserFormat::getUserArr($userIds);

        $relationship = service()->RelationshipService->getRelationshipByIdArr($currentUserId, ArrayHelper::getColumn($dynamics, 'user_id'));
        $result = [];
        foreach ($dynamics as $dynamic) {
            $dynamic = $dynamic->commonFormat();

            if ($dynamic['target_type'] == GlobalConst::TARGET_TYPE_ARTICLE && isset($articleList[$dynamic['target_id']])) {
                $result[] = ArrayHelper::merge($dynamic, [
                    'apply_at' => $dynamic['created_at'],
                    'user' => ArrayHelper::merge($userArr[$dynamic['user_id']]->commonFormat(UserField::DYNAMIC), [
                        'attention_status' => service()->RelationshipService->getAttentionStatusByRelationship($relationship, $currentUserId, $dynamic['user_id'])
                    ]),
                    'article' => $articleList[$dynamic['target_id']],
                ]);
            }

            if ($dynamic['target_type'] == GlobalConst::TARGET_TYPE_NOTE && isset($noteList[$dynamic['target_id']])) {
                $result[] = ArrayHelper::merge($dynamic, [
                    'apply_at' => $dynamic['created_at'],
                    'user' => ArrayHelper::merge($userArr[$dynamic['user_id']]->commonFormat(UserField::DYNAMIC), [
                        'attention_status' => service()->RelationshipService->getAttentionStatusByRelationship($relationship, $currentUserId, $dynamic['user_id'])
                    ]),
                    'note' => $noteList[$dynamic['target_id']],
                ]);
            }
        }

        return $result;
    }

    public static function formatByUserDynamicList($dynamics, $currentUserId)
    {
        $articleList = formats()->ArticleFormat->formatArticleList(ArrayHelper::getColumn($dynamics, 'target_id'), $currentUserId, ArticleFormat::SQUARE_ATTENTION_LIST);
        $articleList = ArrayHelper::index($articleList, 'id');

        $noteList = formats()->NoteFormat->formatNoteList(ArrayHelper::getColumn($dynamics, 'target_id'), $currentUserId, NoteFormat::SQUARE_RECOMMEND_LIST);
        $noteList = ArrayHelper::index($noteList, 'id');

        $userIds = ArrayHelper::getColumn($dynamics, 'user_id');
        $userArr = UserFormat::getUserArr($userIds);

        $relationship = service()->RelationshipService->getRelationshipByIdArr($currentUserId, ArrayHelper::getColumn($dynamics, 'user_id'));

        $result = [];
        foreach ($dynamics as $dynamic) {
            $dynamic = $dynamic->commonFormat();

            if ($dynamic['target_type'] == GlobalConst::TARGET_TYPE_ARTICLE && isset($articleList[$dynamic['target_id']])) {
                $result[] = ArrayHelper::merge($dynamic, [
                    'apply_at' => $dynamic['created_at'],
                    'user' => ArrayHelper::merge($userArr[$dynamic['user_id']]->commonFormat(UserField::DYNAMIC), [
                        'attention_status' => service()->RelationshipService->getAttentionStatusByRelationship($relationship, $dynamic['user_id'], $currentUserId)
                    ]),
                    'article' => $articleList[$dynamic['target_id']],
                ]);
            }

            if ($dynamic['target_type'] == GlobalConst::TARGET_TYPE_NOTE && isset($noteList[$dynamic['target_id']])) {
                $result[] = ArrayHelper::merge($dynamic, [
                    'apply_at' => $dynamic['created_at'],
                    'user' => ArrayHelper::merge($userArr[$dynamic['user_id']]->commonFormat(UserField::DYNAMIC), [
                        'attention_status' => service()->RelationshipService->getAttentionStatusByRelationship($relationship, $dynamic['user_id'], $currentUserId)
                    ]),
                    'note' => $noteList[$dynamic['target_id']],
                ]);
            }
        }

        return $result;
    }

    public static function getDynamicArr($dynamicIds, $userIds, $selectScene = '')
    {
        $userIdTable = IdBuilder::getSubTable($userIds);

        $dynamicArr = [];
        foreach ($userIdTable as $tableCount => $userIdArr) {
            $models = SysDynamic::setTableNameByTableCount($tableCount)
                ->whereIn('dynamic_id', (array)$dynamicIds)
                ->get();
            $dynamicArr = ArrayHelper::merge($dynamicArr, $models);
        }

        return $dynamicArr;
    }
}
